/** @file         stack.c
 *  @brief        简要说明
 *  @details      详细说明
 *  @author       lzm
 *  @date         2021-09-06 09:42:22
 *  @version      v1.0
 *  @copyright    Copyright By lizhuming, All Rights Reserved
 *  @blog         https://www.cnblogs.com/lizhuming/
 *
 **********************************************************
 *  @LOG 修改日志:
 **********************************************************
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 指针式
typedef int se_type; /* 元素类型 */
typedef struct
{
    se_type *top;           /* 栈顶指针 */
    se_type *bottom;        /* 栈底指针 */
    se_type stack_size;     /* 栈的最大容量，元素个数。 */
}stack_t;


/**
 * @name   stack_creat
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
stack_t *stack_creat(int size)
{
    stack_t *stack_ptr = NULL;

    stack_ptr = (stack_t *)malloc(sizeof(stack_t));
    if(stack_ptr == NULL)
        return NULL;
    memset(stack_ptr, 0x00, sizeof(stack_t));

    stack_ptr->bottom = (se_type *)malloc(size * sizeof(se_type));
    if(stack_ptr->bottom == NULL)
    {
        free(stack_ptr);
        return NULL;
    }
    memset(stack_ptr->bottom, 0x00, size * sizeof(se_type));

    stack_ptr->top = stack_ptr->bottom;
    stack_ptr->stack_size = size;

    return stack_ptr;
}

/**
 * @name   stack_destroy
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_destroy(stack_t *stack)
{
    if(stack != NULL)
    {
        if(stack->bottom != NULL)
            free(stack->bottom);

        free(stack);

        return 0;
    }

    return -1;
}

/**
 * @name   stack_clear
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_clear(stack_t *stack)
{
    if(stack == NULL)
        return -1;

    stack->top = stack->bottom;

    return 0;
}

/**
 * @name   stack_empty
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_empty(stack_t *stack)
{
    if(stack == NULL)
        return -1;

    if(stack->top == stack->bottom)
        return 1;
    
    return 0;
}

/**
 * @name   stack_full
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_full(stack_t *stack)
{
    if(stack == NULL)
        return -1;

    if(stack->top == (stack->bottom + stack->stack_size * sizeof(se_type)))
        return 1;
    
    return 0;
}

/**
 * @name   stack_length
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_length(stack_t *stack)
{
    if(stack == NULL)
        return -1;

    return (stack->top - stack->bottom)/sizeof(se_type);
}

/**
 * @name   stack_push
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_push(stack_t *stack, se_type elem)
{
    if(stack_full(stack) != 0)
        return -1;

    *stack->top = elem;
    stack->top += sizeof(se_type);

    return 0;
}

/**
 * @name   stack_pop
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_pop(stack_t *stack, se_type *elem)
{
    if(stack_empty(stack) != 0)
    {
        elem = NULL;
        return -1;
    }

    stack->top -= sizeof(se_type);
    elem = stack->top;

    return 0;
}

int main(void)
{
	;
}